<?php
/**
 * Created by PhpStorm.
 * User: ethanzhang
 * Date: 27/12/2016
 * Time: 4:44 PM
 */

namespace App\Utils;
use App\Position;
use Illuminate\Http\Request;
use Exception;

class Store
{
    public static function GetCurPositions()
    {
        return Position::where('expired', '=', 0)->get();
    }

    public static function GetExpiredPositions()
    {
        return Position::where('expired', '=', 1)->get();
    }

    public static function GetTemplates()
    {
        return Position::where('template', '!=', -1)->get();
    }

    public static function GetPositionsById($id)
    {
        return Position::where('id', '=', $id)->get();
    }

    //    Update
    public static function UpdatePosition(Request $request)
    {
        $id = $request->input('id');
        if ($id == NULL) {
            throw new Exception('Parameter id is mandatory');
        }
        $position = Position::where('id', '=', $id)->first();
        if ($position == NULL) {
            throw new Exception('No Position with This ID Existed');
        }
        return self::assemble($request, $position);
    }

    // Add
    public static function CreatePosition(Request $request)
    {
        $position = new Position();
        return self::assemble($request, $position);
    }

    // Delete
    public static function DeletePosition(Request $request)
    {
        $id = $request->input('id');
        if ($id == NULL) {
            throw new Exception('Parameter id is mandatory');
        }
        $position = Position::find($id);
        if ($position == NULL) {
            throw new Exception('No Position with This ID Existed');
        }
        $position->delete();
        return 'success';
    }

    // Private functions
    private static function assemble(Request $request, Position $position)
    {
        $reference      = $request->input('reference');
        $positionTitle  = $request->input('position');
        $type           = $request->input('type');
        $startDate      = $request->input('startDate');
        $monthlySalary  = $request->input('monthlySalary');
        $benefits       = $request->input('benefits');
        $reportTo       = $request->input('reportTo');
        $location       = $request->input('location');
        $responsibility = $request->input('responsibility');
        $requirements   = $request->input('requirements');
        $expired        = $request->input('expired');
        $expiredTime    = $request->input('expiredTime');
        $template       = $request->input('template');
        $templateName   = $request->input('templateName');
        $description    = $request->input('description');

        if ($reference !== NULL) {
            $position->reference = $reference;
        }
        if ($positionTitle !== NULL) {
            $position->position = $positionTitle;
        }
        if ($type !== NULL) {
            $position->type = $type;
        }
        if ($startDate !== NULL) {
            $position->startDate = $startDate;
        }
        if ($monthlySalary !== NULL) {
            $position->monthlySalary = $monthlySalary;
        }
        if ($benefits !== NULL) {
            $position->benefits = $benefits;
        }
        if ($reportTo !== NULL) {
            $position->reportTo = $reportTo;
        }
        if ($location !== NULL) {
            $position->location = $location;
        }
        if ($responsibility !== NULL) {
            $position->responsibility = $responsibility;
        }
        if ($expired !== NULL) {
            $position->expired = $expired;
        }
        if ($requirements !== NULL) {
            $position->requirements = $requirements;
        }
        if ($expiredTime !== NULL) {
            $position->expiredTime = $expiredTime;
        }
        if ($template !== NULL) {
            $position->template = $template;
        }
        if ($templateName !== NULL) {
            $position->templateName = $templateName;
        }
        if ($description !== NULL) {
            $position->description = $description;
        }
        $position->save();
        return $position;
    }
}